package com.lc.demo.service.impl;

import com.lc.demo.bean.Course;
import com.lc.demo.bean.Leavetable;
import com.lc.demo.bean.Signin;
import com.lc.demo.bean.Student;
import com.lc.demo.mapper.CourseMapper;
import com.lc.demo.mapper.LeaveMapper;
import com.lc.demo.mapper.SigninMapper;
import com.lc.demo.mapper.StudentMapper;
import com.lc.demo.result.ResponseMessage;
import com.lc.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Student login(String username, String password) {
        return studentMapper.selectStudentByUsernameAndPaassword(username, password);
    }


    @Override
    public Student selectByUsername(String username) {
        return studentMapper.selectByUsername(username);
    }

    @Override
    public void addStudent(Student student) {
        studentMapper.addStudent(student);
    }

    @Override
    public void delStudentByUsername(String username) {
        studentMapper.delStudentByUsername(username);
    }

    @Override
    public List<Student> getStudentList() {
        return studentMapper.getStudentList();
    }

    @Override
    public void updateStudent(Student student) {
        studentMapper.updateStudent(student);
    }

    @Autowired
    private SigninMapper signinMapper;
    @Autowired
    private LeaveMapper leaveMapper;


    @Override
    public List<Signin> findSignList(Student student) {

        //获取学生id
        String username = student.getUsername();
        //根据学生id查询出所有课表签到数据
        return signinMapper.findSignListByStudentId(username);

    }

    @Override
    public void leave(Leavetable leavetable) {
        //未审核
        leavetable.setState(0);
        Integer courseid = leavetable.getCourseid();
        Course course = courseMapper.selectByPrimaryKey(courseid);
        leavetable.setCoursename(course.getName());

        Student student = studentMapper.selectByUsername(leavetable.getStudentid());
        leavetable.setStudentname(student.getName());
        leaveMapper.insertSelective(leavetable);
    }

    @Autowired
    CourseMapper courseMapper;

    @Override
    public ResponseMessage signin(Signin signin) {
        //判断 当前日期
        Date daytime = signin.getDaytime();
        SimpleDateFormat yyddmm = new SimpleDateFormat("yyyy-MM-dd");
        String format = yyddmm.format(daytime);
        String formatNow = yyddmm.format(new Date());

        //判断当前时间
        Course course = courseMapper.selectByPrimaryKey(signin.getCourseid());
        SimpleDateFormat yyddmmHHmmss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatNowTimeBegin = yyddmmHHmmss.format(course.getBegintime());
        String formatNowTimeEnd = yyddmmHHmmss.format(course.getEndtime());
        String formatNowTime = yyddmmHHmmss.format(new Date());


        if (!(format.equals(formatNow) &&
                (formatNowTime.compareToIgnoreCase(formatNowTimeBegin) >= 0) &&
                (formatNowTime.compareToIgnoreCase(formatNowTimeEnd) <= 0))) {
            return ResponseMessage.errorWithMessage("课程不在签到时间内,无法签到");
        }
        //签到
        signin.setState(1);
        signinMapper.updateByPrimaryKeySelective(signin);
        return null;
    }


}
